package org.tio.im.server.command.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.im.common.ImAio;
import org.tio.im.common.ImPacket;
import org.tio.im.common.ImSessionContext;
import org.tio.im.common.ImStatus;
import org.tio.im.common.packets.ChatBody;
import org.tio.im.common.packets.ChatType;
import org.tio.im.common.packets.Client;
import org.tio.im.common.packets.Command;
import org.tio.im.common.packets.RespBody;
import org.tio.im.common.utils.ChatKit;
import org.tio.im.common.utils.ImKit;
import org.tio.im.server.command.AbCmdHandler;
import org.tio.utils.lock.SetWithLock;

/**
 * 版本: [1.0] 功能说明: 作者: WChao 创建时间: 2017年9月22日 下午2:58:59
 */
public class ChatReqHandler extends AbCmdHandler {
	private static Logger log = LoggerFactory.getLogger(ImAio.class);

	@Override
	public ImPacket handler(ImPacket packet, ChannelContext channelContext) throws Exception {
		if (packet.getBody() == null) {
			throw new Exception("body is null");
		}
		ChatBody chatBody = ChatKit.toChatBody(packet.getBody(), channelContext);

		if (chatBody == null) {// 聊天数据格式不正确
			ImPacket respChatPacket = ChatKit.dataInCorrectRespPacket(channelContext);
			return respChatPacket;
		}
		/**
		 * 当是系统时不校验
		 */
		if (chatBody.getType() == null || !chatBody.getType().equals("sysUser")) {
			ImSessionContext imSessionContext = (ImSessionContext) channelContext.getAttribute();
			Client client = imSessionContext.getClient();
			if (client.getUser() == null) {
				RespBody chatDataInCorrectRespPacket = new RespBody(Command.COMMAND_CHAT_RESP, ImStatus.C202);
				ImPacket respPacket = ImKit.ConvertRespPacket(chatDataInCorrectRespPacket, channelContext);
				respPacket.setSynSeq(packet.getSynSeq());
				respPacket.setStatus(ImStatus.C1);

				return respPacket;
			}
		}
		ImPacket chatPacket = new ImPacket(Command.COMMAND_CHAT_REQ,new RespBody(Command.COMMAND_CHAT_REQ, chatBody).toByte());
		chatPacket.setSynSeq(packet.getSynSeq());// 设置同步序列号;
		if (chatBody.getChatType() == null || ChatType.CHAT_TYPE_PRIVATE.getNumber() == chatBody.getChatType()) {// 私聊
			SetWithLock<ChannelContext> toChannleContexts = ImAio
					.getChannelContextsByUserid(channelContext.getGroupContext(), chatBody.getTo());
			if (toChannleContexts != null && toChannleContexts.size() > 0) {
				ImAio.send(toChannleContexts, chatPacket);
				return ChatKit.sendSuccessRespPacket(channelContext);// 发送成功响应包
			} else {
				return ChatKit.offlineRespPacket(channelContext);// 用户不在线响应包
			}
		} else if (ChatType.CHAT_TYPE_PUBLIC.getNumber() == chatBody.getChatType()) {// 群聊
			/*String group_id = chatBody.getGroup_id();
			ImAio.sendToGroup(channelContext.getGroupContext(), group_id, chatPacket);
			*/
			String group = chatBody.getGroup_id();
			String userId = chatBody.getFrom();
			log.info("group:"+group+",userId:"+userId);
			ImAio.sendToShopGroup(channelContext, group, userId, chatBody);
			return ChatKit.sendSuccessRespPacket(channelContext);// 发送成功响应包
		} else if (ChatType.CHAT_TYPE_SHOP.getNumber() == chatBody.getChatType()) {
			// 群组id,也是商家id
			String group = chatBody.getGroup_id();
			String userId = chatBody.getFrom();
			log.info("服务器商家发送消息，不需要登录就可以发，group:" + group + ",userId:" + userId);
			ImAio.sendToShopUserGroup(channelContext, group, chatBody);
			RespBody chatStatusPacket = new RespBody(Command.COMMAND_CHAT_RESP, ImStatus.C1);
			ImPacket respPacket = ImKit.ConvertRespPacket(chatStatusPacket, channelContext);
			respPacket.setSynSeq(packet.getSynSeq());
			respPacket.setStatus(ImStatus.C1);
			return respPacket;
		}
		return null;
	}

	@Override
	public Command command() {
		return Command.COMMAND_CHAT_REQ;
	}
}
